﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PluginOffice
{
    public class PluginOffice:Cataloger.Plugins.IPlugin
    {
		#region Constructors (1) 

        public PluginOffice()
        {
            rozszerzenia = new Dictionary<string, bool>();
            rozszerzenia.Add(".doc", true);
            rozszerzenia.Add(".docx", true);
            rozszerzenia.Add(".xls", true);
            rozszerzenia.Add(".xlsx", true);
            rozszerzenia.Add(".ppt", true);
            rozszerzenia.Add(".pptx", true);
        }

		#endregion Constructors 

        #region IPluginOpis Members
        Dictionary<string, bool> rozszerzenia = new Dictionary<string, bool>();
        public Dictionary<string,bool> Rozszerzenia
        {
            get 
            {
                return rozszerzenia;
            }

           
        }
        
        public bool UstawOpis(Cataloger.Plik p, System.IO.FileInfo path)
        {
            foreach (KeyValuePair<string,bool> item in Rozszerzenia)
            {
                if (item.Key == path.Extension.ToLower() && item.Value == true)
                {
                    int przesuniecie = 20;
                    StringBuilder opis = new StringBuilder();
                    try
                    {
                        DSOFile.OleDocumentPropertiesClass office = new DSOFile.OleDocumentPropertiesClass();
                        office.Open(path.FullName, true, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
                        DSOFile.SummaryProperties sum = office.SummaryProperties;
                        try
                        {
                            if(sum.ApplicationName!="")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "ApplicationName", sum.ApplicationName));
                        }
                        catch {}
                        try
                        {
                            if (sum.Author != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Author", sum.Author));
                        }
                        catch { }
                        try
                        {
                            if (sum.Category!=null&&sum.Category != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Category", sum.Category));
                        }
                        catch { }
                        try
                        {
                            if (sum.CharacterCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "CharacterCount", sum.CharacterCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.CharacterCountWithSpaces.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "CharacterCountWithSpaces", sum.CharacterCountWithSpaces.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.Comments!=null&&sum.Comments != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Comments", sum.Comments));
                        }
                        catch { }
                        try
                        {
                            if (sum.Company != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Company", sum.Company));
                        }
                        catch { }
                        try
                        {
                            if (sum.DateCreated.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "DateCreated", sum.DateCreated.ToString()));
                        }
                        catch { }
                        try
                        {
                            if (sum.DateLastPrinted.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "DateLastPrinted", sum.DateLastPrinted.ToString()));
                        }
                        catch { }
                        try
                        {
                            if (sum.DateLastSaved.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "DateLastSaved", sum.DateLastSaved.ToString()));
                        }
                        catch { }
                        try
                        {
                            if (sum.HiddenSlideCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "HiddenSlideCount", sum.HiddenSlideCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.Keywords!=null&&sum.Keywords != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Keywords", sum.Keywords));
                        }
                        catch { }
                        try
                        {
                            if (sum.LastSavedBy != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "LastSavedBy", sum.LastSavedBy));
                        }
                        catch { }

                        try
                        {
                            if (sum.Manager!=null&&sum.Manager != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Manager", sum.Manager));
                        }
                        catch { }
                        try
                        {
                            if (sum.SlideCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "SlideCount", sum.SlideCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.LineCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "LineCount", sum.LineCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.WordCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "WordCount", sum.WordCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.MultimediaClipCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "MultimediaClipCount", sum.MultimediaClipCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.NoteCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "NoteCount", sum.NoteCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.PageCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "PageCount", sum.PageCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.ParagraphCount.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "ParagraphCount", sum.ParagraphCount.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.PresentationFormat!=null&&sum.PresentationFormat != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "PresentationFormat", sum.PresentationFormat));
                        }
                        catch { }
                        try
                        {
                            if (sum.RevisionNumber!=null&&sum.RevisionNumber != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "RevisionNumber", sum.RevisionNumber));
                        }
                        catch { }
                        try
                        {
                            if (sum.SharedDocument.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "SharedDocument", sum.SharedDocument.ToString()));
                        }
                        catch { }

                        try
                        {
                            if (sum.Subject != null&sum.Subject!="")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Subject", sum.Subject));
                        }
                        catch { }
                        try
                        {
                            if (sum.Template!=null&&sum.Template != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Template", sum.Template));
                        }
                        catch { }
                        try
                        {
                            if (sum.Title != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Title", sum.Title));
                        }
                        catch { }
                        try
                        {
                            if (sum.TotalEditTime.ToString() != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "TotalEditTime (mins)", sum.TotalEditTime.ToString("N0")));
                        }
                        catch { }
                        try
                        {
                            if (sum.Version != "")
                                opis.AppendLine(string.Format("{0,-28}:{1}", "Version", sum.Version.ToString()));
                        }
                        catch { }

                        foreach (DSOFile.CustomProperty it in office.CustomProperties)
                        {
                            try
                            {
                                opis.AppendLine(string.Format("{0,-28}:{1}", it.Name, it.get_Value().ToString()));
                            }
                            catch { }
                        }


                        office.Close(false);
                    }
                    catch { }
                    p.Opis+=opis.ToString();
                    return true;
                }
                return false;
            }
            return false;

        }

        #endregion

        #region IPlugin Members

        public string Autor
        {
            get { return "Paweł Stasiak"; }
        }

        public string Opis
        {
            get { return "Plugin pobiera opis z plików MsOffice"; }
        }

        public string Wersja
        {
            get { return "1.0"; }
        }

        public void Konfiguruj()
        {
            Form1 f1 = new Form1(Rozszerzenia);
            if (f1.ShowDialog() == DialogResult.OK)
            {
                foreach (KeyValuePair<string, bool> item in f1.pofiice)
                {
                    Rozszerzenia[item.Key] = item.Value;
                }
            }
            
        }


        #endregion

        #region IPlugin Members


        public string Nazwa
        {
            get { return "Plugin Office"; }
        }

        public string OpisToolTip
        {
            get { return "Plugin office pobiera opis z plików office"; }
        }

        #endregion

        #region IPlugin Members

      

        #endregion
    }
}
